<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2019/5/7
 * Time: 16:24
 */

namespace app\admin\controller;


use think\Db;
use think\Request;

class Awardcoupon extends Base
{
    /**
     * 对战连胜获取的优惠券展示
     * @return mixed|\think\response\Json
     */
    public function awardCouponIndex(){
        if(request()->isAjax ()){
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('fight_continue_coupon')
                ->join('up_coupon', 'continue_coupon = coupon_id', 'left')
                ->count();// 获取总条数
            $lists = Db::name('fight_continue_coupon')
                ->join('up_coupon', 'continue_coupon = coupon_id', 'left')
                ->field('continue_id, coupon_name, coupon_online')
                ->page($Nowpage,$limits)
                ->select();
            // 被删除或下架的优惠券不显示
            foreach($lists as $k => $v){
                if($v['coupon_online'] != 1){
                    $lists[$k]['coupon_name'] = '';
                }
            }
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch('awardcoupon/awardcouponindex');
    }
    
    /**
     * 对战连胜获取的优惠券  编辑
     * @return mixed|\think\response\Json
     */
    public function awardCouponEdit(){
        if(Request::instance()->isPost()){
            $data['continue_coupon'] = input('continue_coupon');
            $id = input('continue_id');
            $res = Db::table('up_fight_continue_coupon')->where('continue_id', $id)->update($data);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $id = input('id');
        $info = Db::table('up_fight_continue_coupon')->where('continue_id', $id)->find();
        $coupon = Db::table('up_coupon')
            ->field('coupon_id, coupon_name')
            ->where('coupon_status', 1)
            ->where('coupon_type', 2)
            ->where('coupon_online', 1)
            ->where('coupon_num', $id)
            ->select();
        $this->assign([
            'info' => $info,
            'coupon' => $coupon,
            'id' => $id
        ]);
        return $this->fetch('awardcoupon/awardcouponedit');
    }
}